<?php
/**
 * User: hjd-yao
 * Date: 2019/4/2
 * Time: 16:38
 */

namespace DC\UserCenter\Services;


use App\Helpers\SelfEncrypt;
use App\Models\Bonus\BonusRecordModel;
use App\Models\Rule\BonusRuleModel;
use DC\BonusCenter\Services\RegBonus;
use DC\Support\Traits\DCResponse;
use DC\UserCenter\Models\UserAuthModel;
use DC\UserCenter\Models\UserBaseModel;
use DC\UserCenter\Models\UserInfoModel;
use DC\UserCenter\Models\UserWalletModel;
use Illuminate\Http\Request;

class UserRegister
{
    use DCResponse;
    public function register(Request $request)
    {
        /*基本信息*/
        $user_member = $request->post('user_number')??SelfEncrypt::createSn('kx');
        $userBaseModel = new UserBaseModel();
        $userBaseModel->user_number = $user_member;
        $userBaseModel->real_name = $request->post('real_name');
        $userBaseModel->user_phone = $request->post('user_phone');
        $userBaseModel->nickname = $request->post('nickname');
        $userBaseModel->user_email = $request->post('user_email',$user_member.'@qq.com');
        $userBaseModel->referrer_code = strtoupper(SelfEncrypt::randStr(12));
        $userBaseModel->status = 1;
        $userBaseModel->activated_at = time();
        $userBaseModel->is_real_name = 1;
        $userBaseModel->level = $request->post('level',0);

//        $userBaseModel->star_level = $request->post('star_level',0);
        $userBaseModel->reg_type = $request->post('reg_type',0);
        $userBaseModel->is_bill = $request->post('is_bill',0);
        $userBaseModel->is_partner = $request->post('is_partner',0);
        $userBaseModel->is_out=0;
        $data = $userBaseModel->checkReferrer($request->post('referrer',0));
        if(!$data){
            return $this->error($userBaseModel->getErrorMsg());
        }
        $userBaseModel->referrer_id = $data['referrer_id'];
        $userBaseModel->referrer_ids = $data['referrer_ids'];
        $userBaseModel->tdeep = $data['tdeep'];

        $data_j = $userBaseModel->checkJiedian($request->post('jiedian',0));
        if(!$data_j){
            return $this->error($userBaseModel->getErrorMsg());
        }
        $userBaseModel->jiedian_id = $data_j['jiedian_id'];
        $userBaseModel->jiedian_ids = $data_j['jiedian_ids'];
        $userBaseModel->jdeep = $data_j['jdeep'];
//        $userBaseModel->right_id = $data_j['right_id'];
//        $userBaseModel->leftright = $data_j['leftright'];
//        $userBaseModel->jiedian_lids = $data_j['jiedian_lids'];
//        $userBaseModel->jiedian_rids = $data_j['jiedian_rids'];

//        //报单人信息
//        $data_b = $userBaseModel->checkBd($request->post('bdr',0));
//        if(!$data_b){
//            return $this->error($userBaseModel->getErrorMsg());
//        }
//        $userBaseModel->bd_uid = $data_b['bd_uid'];


        if(!$userBaseModel->save()){
            return $this->error('会员添加失败');
        }
        /*登录信息*/
        $userAuth = UserAuthModel::init()->handleData($request->post());
        try{
            $userBaseModel->auth()->save(new UserAuthModel($userAuth));
        } catch (\Exception $e){
            $userBaseModel->where('member_id',$userBaseModel->member_id)->delete();
            return $this->error($e->getMessage());
        }
        /*账户信息*/
        $userBaseModel->wallet()->save(new UserWalletModel(['user_number'=>$user_member,'status'=>1]));

        /*详细信息*/
        $info_data = $request->post();
        $info_data['user_number'] = $user_member;
        $userInfo = UserInfoModel::init()->handleData($info_data);
        $userBaseModel->info()->save(new UserInfoModel($userInfo));

        //节点人节点人数加1
        UserBaseModel::where('member_id',$data_j['jiedian_id'])->increment('num_zj');

        return $this->success(['id'=>$userBaseModel->member_id]);
    }

    public function edit($input_data,$id, $action='base')
    {
        if ($action == 'base'){
            return $this->editBase($input_data,$id);
        }else{
            return $this->editInfo($input_data,$id);
        }

    }

    private function editBase($input_data,$id)
    {
        $info = UserBaseModel::find($id);
        if(!$info){
            return $this->error('修改信息不存在');
        }
        $is_can_update = false;
        if(isset($input_data['real_name'])&&
             $info->real_name != $input_data['real_name']
         ){
             $is_can_update = true;
             $info->real_name = $input_data['real_name'];
         }

        if(isset($input_data['nickname'])&&
            $info->nickname != $input_data['nickname']
        ){
            $is_can_update = true;
            $info->nickname = $input_data['nickname'];
        }

        if(isset($input_data['head_pic'])&&
            $info->head_pic != $input_data['head_pic']
        ){
            $is_can_update = true;
            $info->head_pic = $input_data['head_pic'];
        }

        if(isset($input_data['qianming_pic'])&&
            $info->qianming_pic != $input_data['qianming_pic']
        ){
            $is_can_update = true;
            $info->qianming_pic = $input_data['qianming_pic'];
        }

        if(isset($input_data['user_phone'])
            &&$info->user_phone!=$input_data['user_phone']
        ){
            $is_can_update = true;
            $info->user_phone = $input_data['user_phone'];
        }
        if(isset($input_data['user_email'])
            &&$info->user_email != $input_data['user_email']
        ){
            $is_can_update = true;
            $info->user_email = $input_data['user_email'];
        }

        if(isset($input_data['gz_day_can'])
            &&$info->gz_day_can != $input_data['gz_day_can']
        ){
            $is_can_update = true;
            $info->gz_day_can = $input_data['gz_day_can'];
        }
        if(isset($input_data['gz_day_had'])
            &&$info->gz_day_had != $input_data['gz_day_had']
        ){
            $is_can_update = true;
            $info->gz_day_had = $input_data['gz_day_had'];
        }

        if(isset($input_data['gz_day_val'])
            &&$info->gz_day_val != $input_data['gz_day_val']
        ){
            $is_can_update = true;
            $info->gz_day_val = $input_data['gz_day_val'];
        }

        if(isset($input_data['level'])
            &&$info->level!=$input_data['level']
        ){
            $is_can_update = true;
            $info->level = $input_data['level'];
        }
        if(isset($input_data['fs_level'])
            &&$info->fs_level!=$input_data['fs_level']
        ){
            $is_can_update = true;
            $info->fs_level = $input_data['fs_level'];
        }
        if(isset($input_data['cd_level'])
            &&$info->cd_level!=$input_data['cd_level']
        ){
            $is_can_update = true;
            $info->cd_level = $input_data['cd_level'];
        }

        if(isset($input_data['reg_type'])
            &&$info->reg_type!=$input_data['reg_type']
        ){
            $is_can_update = true;
            $info->reg_type = $input_data['reg_type'];
        }

        if(isset($input_data['is_partner'])
            &&$info->is_partner!=$input_data['is_partner']
        ){
            $is_can_update = true;
            $info->is_partner = $input_data['is_partner'];
        }

        if(isset($input_data['is_bill'])
            &&$info->is_bill!=$input_data['is_bill']
        ){
            $is_can_update = true;
            $info->is_bill = $input_data['is_bill'];
        }

        if(isset($input_data['is_bd'])
            &&$info->is_bd!=$input_data['is_bd']
        ){
            $is_can_update = true;
            $info->is_bd = $input_data['is_bd'];
        }

        if(isset($input_data['can_fenhong'])
            &&$info->can_fenhong!=$input_data['can_fenhong']
        ){
            $is_can_update = true;
            $info->can_fenhong = $input_data['can_fenhong'];
        }

        if(!$is_can_update){
            return $this->success('无修改信息');
        }
        if(!$info->save()){
            return $this->error('修改失败');
        }
        return $this->success(['id'=>$id]);
    }

    private function editInfo($input_data,$id)
    {
        $info = UserInfoModel::find($id);
        $is_can_update = false;
        if(isset($input_data['user_sex'])
            &&$info->user_sex!=$input_data['user_sex']
        ){
            $is_can_update = true;
            $info->user_sex = $input_data['user_sex'];
        }

        if(isset($input_data['ID_card'])
            &&$info->ID_card!=$input_data['ID_card']
        ){
            $is_can_update = true;
            $info->ID_card = $input_data['ID_card'];
        }


        if(isset($input_data['ID_card_front'])
            &&$info->ID_card_front!=$input_data['ID_card_front']
        ){
            $is_can_update = true;
            $info->ID_card_front = $input_data['ID_card_front'];
        }


        if(isset($input_data['ID_card_back'])
            &&$info->ID_card_back!=$input_data['ID_card_back']
        ){
            $is_can_update = true;
            $info->ID_card_back = $input_data['ID_card_back'];
        }

        if(isset($input_data['alipay'])
            &&$info->alipay!=$input_data['alipay']
        ){
            $is_can_update = true;
            $info->alipay = $input_data['alipay'];
        }

        if(isset($input_data['weichat'])
            &&$info->weichat!=$input_data['weichat']
        ){
            $is_can_update = true;
            $info->weichat = $input_data['weichat'];
        }

        if(isset($input_data['receiving_code'])
            &&$info->receiving_code!=$input_data['receiving_code']
        ){
            $is_can_update = true;
            $info->receiving_code = $input_data['receiving_code'];
        }

        if(isset($input_data['bank_card'])
            &&$info->bank_card!=$input_data['bank_card']
        ){
            $is_can_update = true;
            $info->bank_card = $input_data['bank_card'];
        }

        if(isset($input_data['bank_name'])
            &&$info->bank_name!=$input_data['bank_name']
        ){
            $is_can_update = true;
            $info->bank_name = $input_data['bank_name'];
        }

        if(isset($input_data['bank_mobile'])
            &&$info->bank_mobile!=$input_data['bank_mobile']
        ){
            $is_can_update = true;
            $info->bank_mobile = $input_data['bank_mobile'];
        }

        if(isset($input_data['live_area'])
            &&$info->live_area!=arr2str($input_data['live_area'])
        ){
            $is_can_update = true;
            $info->live_area = arr2str($input_data['live_area']);
        }

        if(isset($input_data['live_address'])
            &&$info->live_address!=$input_data['live_address']
        ){
            $is_can_update = true;
            $info->live_address = $input_data['live_address'];
        }

        if(!$is_can_update){
            return $this->success('没有修改任何信息');
        }

        if(!$info->save()){
            return $this->error('修改失败');
        }
        return $this->success(['id'=>$id]);
    }

    // 分享注册
    public function registerByShare(Request $request)
    {
        /*基本信息*/
//        $user_member = SelfEncrypt::createSn('kx');
        $user_member = $request->post('user_number');;
//dd($user_member);
        $userBaseModel = new UserBaseModel();
        $userBaseModel->user_number = $user_member;
        if(empty($request->post('name'))){
            $userBaseModel->real_name = $user_member;
        }else{
            $userBaseModel->real_name = $request->post('name');
        }
        $userBaseModel->user_phone = $request->post('phone');
        $userBaseModel->nickname = $user_member ;
     //   $userBaseModel->nickname = $request->post('nickname');
        $userBaseModel->user_email = $request->post('user_email',$user_member.'@qq.com');
        $userBaseModel->referrer_code = strtoupper(SelfEncrypt::randStr(12));
        $userBaseModel->status = 1;
        $userBaseModel->activated_at = time();
        $userBaseModel->level = $request->post('level',0);
        $userBaseModel->reg_type = 0;
        $userBaseModel->is_real_name = 1;
        $data = $userBaseModel->checkReferrerByCode($request->post('share_code',0));
//        $data = $userBaseModel->checkReferrer($request->post('share_code',0));
        if(!$data){
            return $this->error($userBaseModel->getErrorMsg());
        }
        $userBaseModel->referrer_id = $data['referrer_id'];
        $userBaseModel->referrer_ids = $data['referrer_ids'];
        $userBaseModel->tdeep = $data['tdeep'];

        $data_j = $userBaseModel->checkJiedian($request->post('jiedian',0));
        if(!$data_j){
            return $this->error($userBaseModel->getErrorMsg());
        }
        $userBaseModel->jiedian_id = $data_j['jiedian_id'];
        $userBaseModel->jiedian_ids = $data_j['jiedian_ids'];
        $userBaseModel->jdeep = $data_j['jdeep'];
//        $userBaseModel->right_id = $data_j['right_id'];
//        $userBaseModel->leftright = $data_j['leftright'];
//        $userBaseModel->jiedian_lids = $data_j['jiedian_lids'];
//        $userBaseModel->jiedian_rids = $data_j['jiedian_rids'];

//        //前台注册推荐人就是节点人
//        $userBaseModel->jiedian_id = $data['referrer_id'];
//        $userBaseModel->jiedian_ids = $data['referrer_ids'];
//        $userBaseModel->jdeep = $data['tdeep'];

        //报单人信息
//        $data_b = $userBaseModel->checkBd($request->post('bdr',0));
//        if(!$data_b){
//            return $this->error($userBaseModel->getErrorMsg());
//        }
//        $userBaseModel->bd_uid = $data_b['bd_uid'];
//        //报单人就是推荐人
//        $userBaseModel->bd_uid = $data['referrer_id'];

        if(!$userBaseModel->save()){
            return $this->error('会员添加失败');
        }
        /*登录信息*/
        $data['login_pwd'] = $request->post('login_pwd');
        $data['safe_pwd'] = $request->post('pay_pwd');
        $userAuth = UserAuthModel::init()->handleData($data);
        try{
            $userBaseModel->auth()->save(new UserAuthModel($userAuth));
        } catch (\Exception $e){
            $userBaseModel->where('member_id',$userBaseModel->member_id)->delete();
            return $this->error($e->getMessage());
        }
        /*账户信息*/
        $userBaseModel->wallet()->save(new UserWalletModel(['user_number'=>$user_member,'status'=>1]));

        /*详细信息*/
        $info_data = $request->post();
        $info_data['user_number'] = $user_member;
        $userInfo = UserInfoModel::init()->handleData($info_data);
        $userBaseModel->info()->save(new UserInfoModel($userInfo));

        //节点人节点人数加1
        UserBaseModel::where('member_id',$data_j['jiedian_id'])->increment('num_zj');

        return $this->success(['id'=>$userBaseModel->member_id]);
    }


    // 前台会员帮注册
    public function registerByUser(Request $request)
    {
        $user_id=$request->get('user_id');
        /*基本信息*/
        $user_member = SelfEncrypt::createSn('kx');
        $userBaseModel = new UserBaseModel();
        $userBaseModel->user_number = $user_member;
        if(empty($request->post('name'))){
            $userBaseModel->real_name = $user_member;
        }else{
            $userBaseModel->real_name = $request->post('name');
        }
        $userBaseModel->user_phone = $request->post('phone');
        $userBaseModel->nickname = $user_member ;
        //   $userBaseModel->nickname = $request->post('nickname');
        $userBaseModel->user_email = $request->post('user_email',$user_member.'@qq.com');
        $userBaseModel->referrer_code = strtoupper(SelfEncrypt::randStr(12));
        $userBaseModel->status = 0;
        $userBaseModel->level = $request->post('level',0);
        $userBaseModel->reg_type = 0;
        $userBaseModel->is_real_name = 1;
        $data = $userBaseModel->checkReferrer($request->post('share_code',0),'user_phone');
        if(!$data){
            return $this->error($userBaseModel->getErrorMsg());
        }
        $userBaseModel->referrer_id = $data['referrer_id'];
        $userBaseModel->referrer_ids = $data['referrer_ids'];
        $userBaseModel->tdeep = $data['tdeep'];
        //报单人就是注册人
//        $userBaseModel->bd_uid = $user_id;

        if(!$userBaseModel->save()){
            return $this->error('会员添加失败');
        }
        /*登录信息*/
        $data['login_pwd'] = $request->post('login_pwd');
        $data['safe_pwd'] = $request->post('pay_pwd');
        $userAuth = UserAuthModel::init()->handleData($data);
        try{
            $userBaseModel->auth()->save(new UserAuthModel($userAuth));
        } catch (\Exception $e){
            $userBaseModel->where('member_id',$userBaseModel->member_id)->delete();
            return $this->error($e->getMessage());
        }
        /*账户信息*/
        $userBaseModel->wallet()->save(new UserWalletModel(['user_number'=>$user_member,'status'=>1]));

        /*详细信息*/
        $info_data = $request->post();
        $info_data['user_number'] = $user_member;
        $userInfo = UserInfoModel::init()->handleData($info_data);
        $userBaseModel->info()->save(new UserInfoModel($userInfo));

        return $this->success(['id'=>$userBaseModel->member_id]);
    }

    public function editByUser($input_data,$user_id)
    {
        $res = $this->editBase($input_data,$user_id);
        if(!$res['status']){
            return $this->error('修改失败');
        }
        $res = $this->editInfo($input_data,$user_id);

        return $res;
    }

}